//-----------------------------------------------------------------------------
// Copyright (c) BaFang Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace BaFangSpider
{

    using System;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    /// <summary>
    /// Summary description for HtmlUtility
    /// </summary>
    public class HtmlUtility
    {
        static Regex tag = new Regex("<.*?>");

        public static string GetHtml(string url)
        {
            return GetHtml(url, null);
        }

        public static int GetResponseLength(string url)
        {
            WebClient client = new WebClient();
            //client.Proxy = System.Net.WebProxy.GetDefaultProxy();
            client.Headers.Set("Pragma", "no-cache");
            byte[] buf = client.DownloadData(url);
            if (buf == null)
            {
                return 0;
            }
            return buf.Length;
        }

        static string GetHtml(string url, Encoding encoding)
        {
            WebClient client = new WebClient();
            client.Headers.Set("Pragma", "no-cache");
            byte[] buf = client.DownloadData(url);
            client.Dispose();
            if (encoding != null) return encoding.GetString(buf);
            string html = Encoding.UTF8.GetString(buf);
            encoding = GetEncoding(html);
            if (encoding == null || encoding == Encoding.UTF8) return html;
            return encoding.GetString(buf);
        }

        static Encoding GetEncoding(string html)
        {
            string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
            string charset = Regex.Match(html, pattern).Groups["charset"].Value;
            try { return Encoding.GetEncoding(charset); }
            catch (ArgumentException) { return null; }
        }

        public static string PostHtml(string url, Encoding encoding, string data)
        {
            byte[] dataBuffer = Encoding.ASCII.GetBytes(data);

            WebClient client = new WebClient();
            byte[] buf = client.UploadData(url, dataBuffer);

            client.Dispose();

            if (encoding != null)
            {
                return encoding.GetString(buf);
            }
            string html = Encoding.UTF8.GetString(buf);
            encoding = GetEncoding(html);
            if (encoding == null || encoding == Encoding.UTF8) return html;

            return encoding.GetString(buf);
        }

        static public string EncodeGB2312ToUrl(string input)
        {
            return HttpUtility.UrlEncode(EncodeStringToGB2312Bytes(input));
        }

        static public byte[] EncodeStringToGB2312Bytes(string input)
        {
            Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
            Encoding Unicode = Encoding.Unicode;
            // Because all C# string are unicode encoded.
            byte[] b = Unicode.GetBytes(input);
            byte[] b1 = Encoding.Convert(Unicode, GB2312, b);
            return b1;
        }

        // 
        static public int GetHtmlWordCount(string content, int adjust)
        {
            MatchCollection mc = tag.Matches(content);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mc.Count - 1; i++)
            {
                int last = mc[i].Index + mc[i].Length;
                sb.Append(content.Substring(last, mc[i + 1].Index - last));
            }
            string s = sb.ToString();
            // 40 is the prefix rubbish string.
            return s.Length - adjust;
        }
    }
}
